package com.eamois.dao;

import java.io.Serializable;
import java.util.Date;

import org.courser.Util;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

import com.eamois.domain.BaseEntity;

public class DefaultListener extends EmptyInterceptor {

	private static final long serialVersionUID = 1L;

	@Override
	public boolean onSave(Object object, Serializable id, Object[] state,
			String[] propertyNames, Type[] types) {

		if (object instanceof BaseEntity) {
			BaseEntity entity = (BaseEntity) object;
			Date now = Util.nowFullTime();
			for (int i = 0; i < propertyNames.length; i++) {
				if (propertyNames[i].equals("recordTime")) {

					if (entity.getRecordTime() == null) {
						((BaseEntity) entity).setRecordTime(now);
						state[i] = now;
					}
				}
				if (propertyNames[i].equals("modifyTime")) {
					if (entity.getRecordTime() != null) {
						((BaseEntity) entity).setModifyTime(now);
						state[i] = now;
					}
				}

			}
		}
		return false;
	}
}
